/**
 * XML工具类
 */
package indi.zjh.dp.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class XMLUtil {

	/**
	 * 获取指定xml文件的某个元素的值
	 * @param filepath:
	 * @param path
	 * @return
	 * @throws 
	 */
	public static String getString(String filepath, String path) throws DocumentException, Exception
	{
		SAXReader reader = new SAXReader();
		
		Document document = reader.read(new File(filepath));
		
		path = path.trim();
		Element node = null;
		
		String[] elementPaths = path.split("/");
		if(elementPaths.length <= 1)
		{
			throw new Exception("invalid path");
		}

		int len = elementPaths.length;
		int index = 1;
		String thispath = null;
		String result = null;
		do
		{
			thispath = elementPaths[index];
			if(index == 1)
				node = document.getRootElement();
			else
			{
				List<Element> nsElements = node.elements();
				int i;
				for(i = 0; i < nsElements.size(); ++i)
				{
					if(nsElements.get(i).getName().equals(thispath))
					{
						node = nsElements.get(i);
						break;
					}
				}
				if(i == nsElements.size())
				{
					throw new Exception("Path can not match");
				}
			}
			
			if(node.getName().equals(thispath) && index == len -1)
			{
				result = node.getStringValue().trim();
			}
			
		}while(index++ < len-1);
		return result;


	}
	
	/**
	 * 获取指定xml的数组字符串
	 * @param filepath
	 * @param path
	 * @return
	 */
	public static String[] getStringList(String filepath, String path) throws Exception
	{
		SAXReader reader = new SAXReader();
		
		Document document = reader.read(new File(filepath));
		
		path = path.trim();
		Element node = null;
		
		String[] elementPaths = path.split("/");
		if(elementPaths.length <= 1)
		{
			throw new Exception("invalid path");
		}

		int len = elementPaths.length;
		int index = 1;
		String thispath = null;
		ArrayList<String> result = new ArrayList<String>();
		do
		{
			thispath = elementPaths[index];
			if(index == 1)
				node = document.getRootElement();
			else
			{
				List<Element> nsElements = node.elements();
				
				
				int i;
				for(i = 0; i < nsElements.size(); ++i)
				{
					if(nsElements.get(i).getName().equals(thispath))
					{
						node = nsElements.get(i);
						break;
					}
				}
				if(i == nsElements.size())
				{
					throw new Exception("Path can not match "+thispath);
				}
				
				if(index != len - 2)
				{
					for(i = 0; i < nsElements.size(); ++i)
					{
						if(nsElements.get(i).getName().equals(thispath))
						{
							node = nsElements.get(i);
							result.add(node.getStringValue().trim());
							//break;
						}
					}
					String[] desc = new String[0];
					return result.toArray(desc); 
				}
			}			
		}while(index++ < len-1);
		String[] desc = new String[0];
		return result.toArray(desc);
	}
}
